/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.aggjpsv.libraryserver.entities;

/**
 *
 * @author SergioPeres
 */
public class DataPiles {
    
    private String action;
    private String name;
    private String data;
    private boolean d;
    
    public DataPiles() {}

    //<editor-fold defaultstate="collapsed" desc=" Get ...">
    public String getAction() {
        return action;
    }
    
    public String getName() {
        return name;
    }
    
    public String getData() {
        return data;
    }
    
    public boolean isEmptyData() {
        return !d;
    }
    //</editor-fold>
    
    public boolean setPilesAction(String data) {
        String[] msn = getRegistro(normalizedData(data));
        this.d = false;
        
        String temp;
        for (int i = 0; i < msn.length; i++) {
            temp = getParametro(msn[i]);
            if (isAction(temp)) {
                this.action = getCampo(msn[i]);
            } else if (isName(temp)) {
                this.name = getCampo(msn[i]);
            } else if (isData(temp)) {
                this.data = getCampo(msn[i]);
                this.d = true;
            } else {
                return false;
            }
        }
        
        return true;
    }

    @Override
    public String toString() {
        return "action=" + action + ", name=" + name + ((d) ? ", data=" + data : "");
    }
    
    public String toStringData() {
        return "action=" + action + "&name=" + name + ((d) ? "&data=" + data : "");
    }
    
    private String normalizedData(String data) {
        String temp = "";
        
        for (int i = 0; i < data.length(); i++) {
            if (data.charAt(i) == '+') {
                temp = temp + " ";
            } else {
                temp = temp + data.charAt(i);
            }
        }
        
        return temp;
    }
    
    private String[] getRegistro(String data) {
        return data.split("&");
    }
    
    private String getParametro(String registro) {
        return registro.split("=")[0];
    }
    
    private String getCampo(String registro) {
        return registro.split("=")[1];
    }

    private boolean isAction(String temp) {
        return ( temp.equalsIgnoreCase("action") || temp.equalsIgnoreCase("acao") );
    }
    
    private boolean isName(String temp) {
        return ( temp.equalsIgnoreCase("name") || temp.equalsIgnoreCase("nome") );
    }
    
    private boolean isData(String temp) {
        return ( temp.equalsIgnoreCase("data") 
                || temp.equalsIgnoreCase("dado") || temp.equalsIgnoreCase("dados") );
    }
}
